package com.fbs.wowo.message.service.imp;

import com.fbs.wowo.message.entity.UserMsg;
import com.fbs.wowo.message.mapper.UserMsgMapper;
import com.fbs.wowo.message.service.UserMsgService;
import com.fbs.wowo.util.common.Generator;
import com.fbs.wowo.util.rongcloud.RCUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("userMsgService")
@Transactional
public class UserMsgServiceImp implements UserMsgService {
    /**
     * 消息列表
     *
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<UserMsg> list(String userId, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<UserMsg> list = userMsgMapper.findByUserId(userId);
        return list;
    }

    /**
     * 插入消息
     *
     * @param record
     */
    @Override
    public void add(UserMsg record) {
        record.setUmid(Generator.generateId());
        record.setStatus((byte) 1);
        record.setCreateTime(new Date());
        userMsgMapper.insertSelective(record);

        // 发送消息
        String[] userIds = {record.getUserId()};
        RCUtils.sysMsg(userIds, record.getMsgContent());
    }

    /**
     * 读消息
     *
     * @param umid
     */
    @Override
    public void read(String umid) {
        UserMsg userMsg = new UserMsg();
        userMsg.setStatus((byte) 2);
        userMsg.setUmid(umid);
        userMsgMapper.updateByPrimaryKeySelective(userMsg);
    }

    /**
     * 删除消息
     *
     * @param userMsgId
     */
    @Override
    public void del(String userMsgId) {
        userMsgMapper.deleteByPrimaryKey(userMsgId);
    }

    @Autowired
    public UserMsgServiceImp(UserMsgMapper userMsgMapper) {
        this.userMsgMapper = userMsgMapper;
    }
    private UserMsgMapper userMsgMapper;
}
